package group2assignone.ucm.customer;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Customer;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.customer.CustomerEditView;
import group2assignone.view.customer.CustomerWelcomeView;
import javax.swing.JOptionPane;

/**
 *Use Case Controller - allows the user to edit their account once
 * they are logged in.
 */
public class UCEditCustomer implements UCController {

    private Customer customer;
    private Customer newCust;
    private boolean ok,  authOk;
    private Authentication auth;
    private CustomerEditView view;
    private int option;

    /**
     * sets the model(customer) and view
     * @param customer
     * @param view
     */
    public UCEditCustomer(Customer customer, CustomerEditView view) {
        this.customer = customer;
        this.view = view;
    }

    /**
     * Use Case: Create Customer Account
     * Pre-Condition: Edit Customer UI
     * Post-Condition: UI is updated
     * 1. User inputs new account details
     * 2. User selects 'Edit Account'
     * 3. System loads new account details
     * 4. System verifies details
     * 5. System updates account
     * 6. System updates the UI
     * 4a. - user enters invalid detail type
     *     - system notifies user and prompts for new entry
     */
    public void run() {
        newCust = new Customer();
        auth = new Authentication();

        option = view.confirmEdit();
        if (option == JOptionPane.YES_OPTION) {
            view.upLoad(newCust);
            authOk = auth.checkCustFields(newCust);
            if (authOk) {
                MyDAO coms = UCStartup.getInstance().getDAO();
                coms.sQLEditCustomer(newCust, customer.getUsername());
                customer = newCust;
                view.notifyEdit();
                CustomerWelcomeView gui = new CustomerWelcomeView(newCust);
                gui.go();
                view.dispose();
            } else {
                UITool.displayError(auth.getErrorMsg(), view);
            }
        }
    }
}

